const fs = require('fs')
const log = require('../utils/log')
const path = require('path')
const resolve = file => path.resolve(__dirname, file)

module.exports = name => {
    log('yellow', '开始生成组件')
    const templet = resolve('../templets/component.templet.vue')
    const path = resolve('../../src/components/' + name + '.vue')
    fs.readFile(templet, 'utf-8', function (err, data) {
        if (err) {
            throw err
        }
        const result = data.replace(/\$\{name\}/g, name)
        fs.writeFile(path, result, function (err) {
            if (err) {
                throw err
            }
            log('green', `生成组件/src/components/${name}.vue`)
        })
    })
}